jQuery(document).ready(function(e) {
	
	var columnsToIndex = [1,2], // first column is 1
		liveFilter = jQuery("#liveFilter");
	
	initLiveFilter(liveFilter, columnsToIndex);
});

// Live Filter
function initLiveFilter(liveFilter, columnsToIndex) {
	
	if (typeof liveFilter !== "undefined" && liveFilter.length > 0) {
		
		liveFilter.wrap('<div class="liveFilter"></div>').parent().prepend('<div class="liveFilterContainer"><div class="group"><input type="text" class="liveFilterInput" value="" placeholder="Live Filter" /><a href="#" class="clearField" title="Clear Filter">x</a></div></div><div class="noResults"><strong>Sorry.</strong> There is no match for your filter; please try again.</div>');
		
		var	thisObj = this,
			liveFilterWrapper = liveFilter.parent(),
			liveFilterField = liveFilterWrapper.find("input.liveFilterInput"),
			liveFilterGrid = jQuery("#liveFilter"),
			liveFilterGridRows = liveFilterGrid.find("tr:gt(0)"), // gt(0) prevents the first row (normally a TH) from being included.
			liveFilterClear = liveFilterWrapper.find(".clearField"),
			liveFilterNoResults = liveFilterGrid.prev(),
			liveFilterDataArray = [],
		
			//Create an array and populate it with key codes that should not cause shake effects
	
			characterValidationArray = [8,45,46], //backspace, insert, delete
		
			//Create an array and populate it with key codes that don't trigger an action
		
			characterExclusionArray = [13,20,27,33,34,37,39,35,36,16,17,18,144,145]; // enter, caps, esc, page up, page down, left, right, home, end, shift, ctrl, alt, num lock, scroll lock

		thisObj.init = function(){
			
			if (typeof columnsToIndex !== "undefined" && columnsToIndex.length > 1) {
				
				for (col=0; col<=columnsToIndex.length-1; col++)
				{			
					liveFilterGridRows.children("td:nth-child(" + columnsToIndex[col] + ")").each(function(i){
					
						liveFilterDataArray[liveFilterDataArray.length++] = jQuery(this).text();
					
					});
				}
				
			} else {
				
				liveFilterGridRows.children("td:first-child").each(function(i){
					
					liveFilterDataArray[i] = jQuery(this).text();
					
				});
			}
		
			liveFilterField.on("keyup",function(key){
				
				thisObj.filterRows(key);
				
			});
			
			liveFilterClear.on("click",function(){
		
				thisObj.clearField();
				
				return false;		
			});			
		};
		
		thisObj.clearField = function() {
		
			liveFilterField.val('');
			liveFilterClear.fadeOut(300);
			liveFilterNoResults.slideUp(300);
			liveFilterGridRows.show();
			liveFilterGrid.show();			
		};
		
		thisObj.filterRows = function(key) {
		
			if (jQuery.inArray(key.keyCode,characterExclusionArray) === -1) {
			
				var	liveFilterValue = liveFilterField.val();
	
				if (liveFilterValue !== "") {

					liveFilterClear.fadeIn(300);
	
					rowsToShow = [];
					
					var currentRow = 0;
					
				// Check the value entered against a regular expression matched with the column data. If a match exists add the row to a new array

					for (var i=0; i < liveFilterDataArray.length; i+=1) {
	
						RE = eval("/" + liveFilterValue + "/i");
		
						if (liveFilterDataArray[i].match(RE)) {
							
							rowsToShow.push(currentRow);
							
						}
						
						if(currentRow < liveFilterGridRows.length - 1) {
	
							currentRow++;
							
						} else {
	
							currentRow = 0;
	
						}
					}
				
				// If there are matches, show the grid, hide all the rows and show the selected ones

					if (rowsToShow.length > 0) {

						liveFilterGrid.show();
						liveFilterGridRows.hide();
						
						if (liveFilterNoResults.is(":visible")) {
							liveFilterNoResults.slideUp(150);
						}
						
						for (var i=0; i < rowsToShow.length; i+=1) {

							jQuery(liveFilterGridRows.get(rowsToShow[i])).show();

						}
						
				// If there are NO matches we hide the grid and display an error panel. If an incorrect value is entered while the error panel is visible it shakes itself assuming if it doesn't match any of the excluded values defined in the characterValidationArray
						
					} else {
						
						liveFilterGrid.hide();

						// if the no results panel is shown and the effects queue length is 0 and there are no illegal character presses
												
						if (liveFilterNoResults.is(":visible") && liveFilterNoResults.queue().length === 0 && jQuery.inArray(key.keyCode,characterValidationArray) == -1) {
							
							liveFilterNoResults.effect('shake', {times:3, distance:3}, 100);

						} else {
							liveFilterNoResults.slideDown(150);	
						}
					}
					
				// If the value entered is blank, hide the clear field button, show the grid and all of its rows and hide the no results panel if it is visible
				
				} else {

					thisObj.clearField();
				}
			}		
		};
		
		thisObj.init();
	}
}